﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity.Core.EntityClient;
using System.Linq;
using System.Text;
using System.Web;

namespace FCDB.Entity
{
   public class ObjectContextHelper
    {
       private static ObjectContextManager<FCDBEntities> _objectContextManager;
        private static string _connectionString;

        /// <summary>
        /// Returns the current ObjectContextManager instance. Encapsulated the 
        /// _objectContextManager field to show it as an association on the class diagram.
        /// </summary>
        private static ObjectContextManager<FCDBEntities> ObjectContextManager
        {
            get { return _objectContextManager; }
            set { _objectContextManager = value; }
        }

        /// <summary>
        /// Gets an object context
        /// </summary>
        public static FCDBEntities CurrentObjectContext
        {
            get
            {

                if (System.Web.HttpContext.Current == null)
                {
                    if (ObjectContextManager == null)
                        InstantiateObjectContextManager();
                    return ObjectContextManager.DbContext;
                }
                else
                {
                    if (null == System.Web.HttpContext.Current.Session["$DBContext"] || null == System.Web.HttpContext.Current.Session["$DBContext"] as FCDBEntities)
                    {
                        var connStr = ObjectContextHelper.GetEntityConnectionString();
                        System.Web.HttpContext.Current.Session["$DBContext"] = new FCDBEntities(connStr);
                    }

                    return System.Web.HttpContext.Current.Session["$DBContext"] as FCDBEntities;
                }
            }
        }

       
        /// <summary>
        /// Instantiates a new ObjectContextManager
        /// </summary>
        private static void InstantiateObjectContextManager()
        {
            //create ASP.NET now
            ObjectContextManager = new StaticObjectContextManager<FCDBEntities>();
        }

        /// <summary>
        /// Gets a connection string
        /// </summary>
        /// <returns>Connection string</returns>
        public static string GetEntityConnectionString()
        {
            if (_connectionString == null)
            {
                try
                {
                    var ecsbuilder = new EntityConnectionStringBuilder();
                    ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["FCDBConnection"];
                    ecsbuilder.Provider = settings.ProviderName;
                    String conn = settings.ConnectionString;
                    ecsbuilder.ProviderConnectionString = conn;
                    ecsbuilder.Metadata = @"res://*/FCDBEntity.csdl|res://*/FCDBEntity.ssdl|res://*/FCDBEntity.msl";
                    _connectionString = ecsbuilder.ToString();

                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                }
                finally
                {

                }

            }
            return _connectionString;
        }
    }
}
